package db_utils

import (
	"fmt"
	"strings"
)

type SQLWhereBuilder struct {
	sb         strings.Builder
	groupLevel int
}

func NewSQLWhereBuilder() *SQLWhereBuilder {
	rval := &SQLWhereBuilder{}
	return rval
}

func (this *SQLWhereBuilder) GBegin() *SQLWhereBuilder {
	this.sb.WriteString("(")
	this.groupLevel++
	return this
}

func (this *SQLWhereBuilder) GEnd() *SQLWhereBuilder {
	this.sb.WriteString("}")
	this.groupLevel--
	return this
}

func (this *SQLWhereBuilder) And() *SQLWhereBuilder {
	this.sb.WriteString(" AND ")
	return this
}

func (this *SQLWhereBuilder) Or() *SQLWhereBuilder {
	this.sb.WriteString(" OR ")
	return this
}

func (this *SQLWhereBuilder) Equal(keyFieldName string, val interface{}) *SQLWhereBuilder {
	strVal := ToSQLVal(val)
	this.sb.WriteString(fmt.Sprintf("%s=%s", keyFieldName, strVal))
	return this
}

func (this *SQLWhereBuilder) String() string {
	return this.sb.String()
}
